package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.QueryCycleTimeRuleDetailReq;
import com.sg.dto.base.app.req.QueryCycleTimeRuleListReq;
import com.sg.entity.OmsCycleTimeRule;
import com.sg.service.base.app.MOmsCycleTimeRuleService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsCycleTimeRuleMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsCycleTimeRuleServiceImpl
        extends ServiceImpl<OmsCycleTimeRuleMapper, OmsCycleTimeRule>
        implements MOmsCycleTimeRuleService {

    @Autowired
    OmsCycleTimeRuleMapper omsCycleTimeRuleMapper;

    /**
     * 3-4-05查时段规则列表[2529]
     * gen by moon at 10/14/2022, 9:26:32 AM
     */
    @Trace(operationName = "3-4-05查时段规则列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsCycleTimeRule> queryCycleTimeRuleList(QueryCycleTimeRuleListReq reqDto) {

        try {
            QueryWrapper<OmsCycleTimeRule> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsCycleTimeRule.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsCycleTimeRule.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsCycleTimeRule.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsCycleTimeRule>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-4-05修改时段规则[2528]
     * gen by moon at 10/14/2022, 9:26:32 AM
     */
    @Trace(operationName = "3-4-05修改时段规则")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCycleTimeRule(OmsCycleTimeRule omsCycleTimeRule) {
        omsCycleTimeRule.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsCycleTimeRule.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsCycleTimeRule.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsCycleTimeRule.setReleaseTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsCycleTimeRule);
    }

    /**
     * 3-4-05新增时段规则[2527]
     * gen by moon at 10/14/2022, 9:26:31 AM
     */
    @Trace(operationName = "3-4-05新增时段规则")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addCycleTimeRule(OmsCycleTimeRule omsCycleTimeRule) {
        omsCycleTimeRule.setTimeRuleId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsCycleTimeRule.setIsArchive("FALSE");
        omsCycleTimeRule.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsCycleTimeRule.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsCycleTimeRule.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsCycleTimeRule.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsCycleTimeRule.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsCycleTimeRule.setReleaseTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsCycleTimeRule.setIsValid("FALSE");
        omsCycleTimeRule.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsCycleTimeRule.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsCycleTimeRule)) {
            return omsCycleTimeRule.getTimeRuleId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-4-05查时段规则详情[2361]
     * gen by moon at 10/14/2022, 9:26:30 AM
     */
    @Trace(operationName = "3-4-05查时段规则详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsCycleTimeRule queryCycleTimeRuleDetail(QueryCycleTimeRuleDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsCycleTimeRule> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsCycleTimeRule> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsCycleTimeRule retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
